function basePath(){
    //获取当前网址，如： http://localhost:8080/ems/Pages/Basic/Person.jsp
    var curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： /ems/Pages/Basic/Person.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8080
    var localhostPath = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/ems
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    //获取项目的basePath   http://localhost:8080/ems/
    var basePath=localhostPath+projectName+"/";
    return basePath;
};

jQuery(document).ready(function() {
	var currentOrgcode = '';
	$.ajax({
		async : false,
		url : projectPath + "user/getCurrentUser.do",
		data : {},
		dataType : "json",
		success : function(data) {
			$('#orgSelect').val(data.orgname);
			$('#orgSelect').attr('data', data.orgcode);
			currentOrgcode = data.orgcode;
		}
	});
	initTable();
	
	//绑定机构下拉列表
	bindOrg2('orgSelect', currentOrgcode, "请选择预约机构");
	
//	bindOrg1('orgSelect');

	//绑定机构下拉列表
//	bindOrg("orgSelect","2","0","");
	
//	bindDict("bespeakItem","DM_XKXMLX","");
	
	//项目select
//	$('#bespeakItem').change(function(){
//		var itemno=$('#bespeakItem').val();
//		if(itemno==""){
//			$('#bespeakItems').html("");
//		}else{
//			bindItem("bespeakItems",itemno,"");
//		}
//	});
		
	bindDict1("st","bespeak_period","");
	
		
	App.setPage("bespeakquery");  //Set current page
	App.init(); //Initialise plugins and elements
	
	//列表查询事件
	$('#search').click(function(){
		$('#bookingTable').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
});		

function initTable(){	
	$('#bookingTable').bootstrapTable({
		method: 'post',
	    url: basePath() + 'reserve/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
                	pageNum: params.offset / params.limit + 1,
                	pageSize: params.limit,
                	ORGCODE: $('#orgSelect').attr('data'),
//                	ITEMTYPECODE: $('#bespeakItem').val(),
//                	ITEMID: $('#bespeakItems').val(),
                	ITEMNAME: $('#itemname').val(),
                	RECORDPERIOD: $('#st').val(),
                	RECORDDATE: $('#recordDate').val()
                };
        },
	    columns: [{  
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '80px',
            formatter : function(value, row, index) { 
// 		                return index + 1;  
                var page = $('#bookingTable').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }         
	    }, {
	        field: 'ITEMNAME',
	        title: '预约项目'
	    }, {
	        field: 'RECORDID',
	        visible : false	        
	    }, {	    	
	        field: 'ORGNAME',
	        title: '预约机构'
	        //visible: false,
	    }, {
	        field: 'RECORDDATE',
	        title: '预约日期'
	    }, {
	        field: 'RECORDPERIOD',
	        title: '预约时间'
	    }, {
	        field: 'USERNAME',
	        title: '预约人'
	    }, {
	        field: 'COMPANYNAME',
	        title: '预约公司'
	    },{
            field: 'STATE',
            title: '状态',
            events :stateEvents,
            formatter : function(value, row, index) { 
            	var a=row.RECORDPERIOD;
            	var b=a.indexOf("-");
            	a=a.substring(b+1,a.lenght);
            	var time=row.RECORDDATE+a;
            	var date =new Date(time);
            	var now = new Date();
            	var boo=false;
            	if(date<now){
            		boo=true;
            	}
            	if(value=="0" && boo==true){
            		return [
            			'<button type="button" class="Delete btn btn-danger  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>已超时</button>'
            		].join('');
            	}else if(value=="1"){
            		return [
            			'<button type="button" class="Defined btn btn-success  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-glass"></span>已处理</button>'
            		].join('');
            	}else if(value=="0" && boo==false){
            		return [
            			'<button type="button" class="Edit btn btn-warning  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>未处理</button>'
					].join('');
            	}	
            }
        }]
	});
}

window.stateEvents={
	'click .Edit' : function(e, value, row, index) {
		$.ajax({
			url : basePath() + 'reserve/changeState.do',
			data : {
				"RECORDID" : row.RECORDID,
				"STATE" :"1"
			},
			dataType : "json",
			success : function(data) {
				if (data.result == 1) {
					$('#bookingTable').bootstrapTable(
					('refresh')); // 很重要的一步，刷新url！
				}
			}
		});
	}
		
}